package com.songlanyun.common.exception;


import com.songlanyun.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice(basePackages = "com.songlanyun")
public class GlobalExceptionControllerAdvice
{

    @Value("${spring.profiles.active}")
    private String profilesActive;

    @ExceptionHandler(value = RRException.class)
    public R handleValidException(RRException e)
    {
        log.error("数据校验出现问题{},异常类型：{}", e.getMsg(), e.getClass());

        return R.error(e.getCode(), e.getMsg());

    }

    @ExceptionHandler(value = {Throwable.class,Exception.class})
    public R handleValidException(Throwable e)
    {
        log.error("系统发生未知异常{},异常类型：{}", e.getMessage(), e.getClass());
        if("dev".equals(profilesActive))
        {
            return R.error(BizCodeEnum.UNKNOWN_EXCEPTION.getCode(), e.getMessage());
        }
        else
        {
            return R.error(BizCodeEnum.UNKNOWN_EXCEPTION.getCode(), BizCodeEnum.UNKNOWN_EXCEPTION.getMsg());

        }
    }
}
